namespace SmartSoft.ReportForms
{
    using SmartSoft;
    using SmartSoft.Controls;
    using SmartSoft.Properties;
    using SmartSoft.ADP;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormReportLister : FormControl
    {
        private ToolStripButton btnAdd;
        private ToolStripButton btnClose;
        private ToolStripButton btnDelete;
        private ToolStripButton btnEdit;
        private ToolStripButton btnMake;
        private IContainer components;
        private ColumnHeader Header1;
        private ColumnHeader Header2;
        private LViewControl lstReports;
        private ToolStripSeparator tbrSep01;
        private ToolStrip Toolbars;
        private ToolStripSeparator toolStripSeparator1;

        public FormReportLister(string ps_Title, string ps_ReportGroups)
        {
            this.InitializeComponent();
            this.Text = ps_Title;
            this.FillReports(ps_ReportGroups);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.OnEdit(true);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.OnDelete();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.OnEdit(false);
        }

        private void btnMake_Click(object sender, EventArgs e)
        {
            this.OnMake();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FillReports(string ps_ReportGroups)
        {
            DataTable reportList = new ReportDataAccess().GetReportList(AppInstance.UserName, ps_ReportGroups);
            this.lstReports.Items.Clear();
            int num = 0;
            foreach (DataRow row in reportList.Rows)
            {
                this.lstReports.Items.Add((string) row["Bar"]);
                this.lstReports.Items[num].SubItems.Add((string) row["Bar_E"]);
                this.lstReports.Items[num].SubItems.Add((string) row["Title"]);
                this.lstReports.Items[num].SubItems.Add(row["Title_E"].ToString());
                this.lstReports.Items[num].SubItems.Add(row["FormFile"].ToString());
                this.lstReports.Items[num].SubItems.Add(row["RepoFile"].ToString());
                this.lstReports.Items[num].SubItems.Add((string) row["RepoGrp"]);
                this.lstReports.Items[num].SubItems.Add(row["Series"].ToString());
                this.lstReports.Items[num].SubItems.Add(((bool) row["OnTop"]) ? "1" : "0");
                this.lstReports.Items[num].SubItems.Add(row["Stt0"].ToString());
                this.lstReports.Items[num].SubItems.Add(row["Stt"].ToString());
                num++;
            }
            if (this.lstReports.Items.Count > 0)
            {
                this.lstReports.Items[0].Selected = true;
            }
        }

        private void InitializeComponent()
        {
            System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
            "Thẻ kho",
            "Thẻ kho"}, -1);
            System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(new string[] {
            "Báo cáo tổng hợp nhật - xuất - tồn",
            "Báo cáo tổng hợp nhập - xuất - tồn"}, -1);
            System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem(new string[] {
            "Báo cáo tồn kho",
            "Báo cáo tồn kho"}, -1);
            this.Toolbars = new System.Windows.Forms.ToolStrip();
            this.btnMake = new System.Windows.Forms.ToolStripButton();
            this.tbrSep01 = new System.Windows.Forms.ToolStripSeparator();
            this.btnAdd = new System.Windows.Forms.ToolStripButton();
            this.btnEdit = new System.Windows.Forms.ToolStripButton();
            this.btnDelete = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.btnClose = new System.Windows.Forms.ToolStripButton();
            this.lstReports = new SmartSoft.Controls.LViewControl();
            this.Header1 = new System.Windows.Forms.ColumnHeader();
            this.Header2 = new System.Windows.Forms.ColumnHeader();
            this.Toolbars.SuspendLayout();
            this.SuspendLayout();
            // 
            // Toolbars
            // 
            this.Toolbars.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnMake,
            this.tbrSep01,
            this.btnAdd,
            this.btnEdit,
            this.btnDelete,
            this.toolStripSeparator1,
            this.btnClose});
            this.Toolbars.Location = new System.Drawing.Point(0, 0);
            this.Toolbars.Name = "Toolbars";
            this.Toolbars.Size = new System.Drawing.Size(796, 25);
            this.Toolbars.TabIndex = 0;
            this.Toolbars.Text = "Toolbars";
            // 
            // btnMake
            // 
            this.btnMake.Image = global::SmartSoft.Properties.Resources.MAKE;
            this.btnMake.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnMake.Name = "btnMake";
            this.btnMake.Size = new System.Drawing.Size(69, 22);
            this.btnMake.Text = "Báo cáo";
            this.btnMake.ToolTipText = "Lập báo cáo";
            this.btnMake.Click += new System.EventHandler(this.btnMake_Click);
            // 
            // tbrSep01
            // 
            this.tbrSep01.Name = "tbrSep01";
            this.tbrSep01.Size = new System.Drawing.Size(6, 25);
            // 
            // btnAdd
            // 
            this.btnAdd.Image = global::SmartSoft.Properties.Resources.BALANCE1;
            this.btnAdd.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(58, 22);
            this.btnAdd.Text = "Thêm";
            this.btnAdd.ToolTipText = "Thêm báo cáo (F2)";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // btnEdit
            // 
            this.btnEdit.Image = global::SmartSoft.Properties.Resources.EDIT1;
            this.btnEdit.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnEdit.Name = "btnEdit";
            this.btnEdit.Size = new System.Drawing.Size(46, 22);
            this.btnEdit.Text = "Sửa";
            this.btnEdit.ToolTipText = "Sửa báo cáo (F3)";
            this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
            // 
            // btnDelete
            // 
            this.btnDelete.Image = global::SmartSoft.Properties.Resources.DELETE1;
            this.btnDelete.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(47, 22);
            this.btnDelete.Text = "Xóa";
            this.btnDelete.ToolTipText = "Xóa báo cáo (F8)";
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // btnClose
            // 
            this.btnClose.Image = global::SmartSoft.Properties.Resources.CLOSE;
            this.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(58, 22);
            this.btnClose.Text = "Thoát";
            this.btnClose.ToolTipText = "Thoát (Esc)";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // lstReports
            // 
            this.lstReports.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.lstReports.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.Header1,
            this.Header2});
            this.lstReports.Font = new System.Drawing.Font("Arial", 8.25F);
            this.lstReports.FullRowSelect = true;
            this.lstReports.GridLines = true;
            this.lstReports.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
            listViewItem1.StateImageIndex = 0;
            listViewItem2.StateImageIndex = 0;
            listViewItem3.StateImageIndex = 0;
            this.lstReports.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
            listViewItem1,
            listViewItem2,
            listViewItem3});
            this.lstReports.Location = new System.Drawing.Point(12, 37);
            this.lstReports.MultiSelect = false;
            this.lstReports.Name = "lstReports";
            this.lstReports.Size = new System.Drawing.Size(772, 485);
            this.lstReports.TabIndex = 1;
            this.lstReports.UseCompatibleStateImageBehavior = false;
            this.lstReports.View = System.Windows.Forms.View.Details;
            this.lstReports.ItemActivate += new System.EventHandler(this.lstReports_ItemActivate);
            // 
            // Header1
            // 
            this.Header1.Text = "Tên báo cáo";
            this.Header1.Width = 384;
            // 
            // Header2
            // 
            this.Header2.Text = "Tên tiếng Anh";
            this.Header2.Width = 384;
            // 
            // FormReportLister
            // 
            this.ClientSize = new System.Drawing.Size(796, 539);
            this.Controls.Add(this.lstReports);
            this.Controls.Add(this.Toolbars);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.MinimumSize = new System.Drawing.Size(802, 566);
            this.Name = "FormReportLister";
            this.Toolbars.ResumeLayout(false);
            this.Toolbars.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void lstReports_ItemActivate(object sender, EventArgs e)
        {
            this.OnMake();
        }

        private void OnDelete()
        {
            if (MessageBox.Show(AppInstance.DeleMessage, AppInstance.SoftwareName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ListViewItem item = this.lstReports.Items[this.lstReports.SelectedIndices[0]];
                object text = item.SubItems[10].Text;
                SSDataAccess.Delete("Reports", text);
                this.lstReports.Items.Remove(item);
            }
        }

        private void OnEdit(bool pb_CheckNewRow)
        {
            if (pb_CheckNewRow || (this.lstReports.Items.Count != 0))
            {
                ListViewItem item;
                FormReportEditor editor = new FormReportEditor();
                editor.SetTitle = pb_CheckNewRow;
                if (this.lstReports.Items.Count == 0)
                {
                    this.lstReports.Items.Add("");
                    this.lstReports.Items[0].SubItems.Add("");
                    this.lstReports.Items[0].SubItems.Add("");
                    this.lstReports.Items[0].SubItems.Add("");
                    this.lstReports.Items[0].SubItems.Add("");
                    this.lstReports.Items[0].SubItems.Add("");
                    this.lstReports.Items[0].SubItems.Add("");
                    this.lstReports.Items[0].SubItems.Add("");
                    this.lstReports.Items[0].SubItems.Add("1");
                    this.lstReports.Items[0].SubItems.Add("1");
                    this.lstReports.Items[0].SubItems.Add("0");
                    item = this.lstReports.Items[0];
                }
                else
                {
                    item = this.lstReports.Items[this.lstReports.SelectedIndices[0]];
                }
                if (this.lstReports.Items.Count > 0)
                {
                    editor.IndexNumber = int.Parse(item.SubItems[9].Text);
                    editor.ReportGroups = item.SubItems[6].Text;
                    editor.ReportBar = item.SubItems[0].Text;
                    editor.ReportBarE = item.SubItems[1].Text;
                    editor.ReportTitle = item.SubItems[2].Text;
                    editor.ReportTitleE = item.SubItems[3].Text;
                    editor.FormFile = item.SubItems[4].Text;
                    editor.RepoFile = item.SubItems[5].Text;
                    editor.Series = item.SubItems[7].Text;
                    editor.AlwaysOnTop = item.SubItems[8].Text == "1";
                }
                editor.ShowDialog();
                if (editor.CheckAccepted)
                {
                    if (pb_CheckNewRow)
                    {
                        item = this.lstReports.Items.Add(editor.ReportBar.ToString());
                        item.SubItems.Add(editor.ReportBarE.ToString());
                        item.SubItems.Add(editor.ReportTitle.ToString());
                        item.SubItems.Add(editor.ReportTitleE.ToString());
                        item.SubItems.Add(editor.FormFile.ToString());
                        item.SubItems.Add(editor.RepoFile.ToString());
                        item.SubItems.Add(editor.ReportGroups.ToString());
                        item.SubItems.Add(editor.Series.ToString());
                        item.SubItems.Add(editor.AlwaysOnTop ? "1" : "0");
                        item.SubItems.Add(editor.IndexNumber.ToString());
                        item.SubItems.Add("0");
                    }
                    else
                    {
                        item.SubItems[0].Text = editor.ReportBar.ToString();
                        item.SubItems[1].Text = editor.ReportBarE.ToString();
                        item.SubItems[2].Text = editor.ReportTitle.ToString();
                        item.SubItems[3].Text = editor.ReportTitleE.ToString();
                        item.SubItems[4].Text = editor.FormFile.ToString();
                        item.SubItems[5].Text = editor.RepoFile.ToString();
                        item.SubItems[6].Text = editor.ReportGroups.ToString();
                        item.SubItems[7].Text = editor.Series.ToString();
                        item.SubItems[8].Text = editor.AlwaysOnTop ? "1" : "0";
                        item.SubItems[9].Text = editor.IndexNumber.ToString();
                    }
                    object text = item.SubItems[10].Text;
                    ReportDataAccess access = new ReportDataAccess();
                    if (access.SaveReportRow(pb_CheckNewRow, ref text, Convert.ToInt16(item.SubItems[9].Text), item.SubItems[0].Text, item.SubItems[1].Text, item.SubItems[2].Text, item.SubItems[3].Text, item.SubItems[4].Text, item.SubItems[5].Text, item.SubItems[6].Text, item.SubItems[7].Text, item.SubItems[8].Text == "1") == 1)
                    {
                        item.SubItems[10].Text = text.ToString();
                    }
                }
            }
        }

        private void OnMake()
        {
            if (this.lstReports.SelectedIndices.Count > 0)
            {
                string str = "SmartSoft.ReportForms.";
                ListViewItem item = this.lstReports.Items[this.lstReports.SelectedIndices[0]];
                System.Type type = System.Type.GetType(str + ("Form" + item.SubItems[4].Text + "Viewer"));
                if (type != null)
                {
                    FormReportViewer viewer = Activator.CreateInstance(type) as FormReportViewer;
                    if (viewer != null)
                    {
                        viewer.TitleText = item.SubItems[0].Text;
                        viewer.ReportTitle = item.SubItems[2].Text;
                        viewer.ReportFile = item.SubItems[5].Text;
                        viewer.ReportSerial = item.SubItems[7].Text;
                        viewer.Show();
                    }
                }
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((keyData == Keys.Return) && this.btnMake.Enabled)
            {
                this.OnMake();
                return true;
            }
            if ((keyData == Keys.F2) && this.btnAdd.Enabled)
            {
                this.OnEdit(true);
            }
            else if ((keyData == Keys.F3) && this.btnEdit.Enabled)
            {
                this.OnEdit(false);
            }
            else if ((keyData == Keys.F8) && this.btnDelete.Enabled)
            {
                this.OnDelete();
            }
            else if (keyData == Keys.Escape)
            {
                base.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}

